<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Product_model extends MY_Model{

    function  __construct(){
        parent::__construct();
        $this->_table         = TBL_PRODUCT;
        $this->primary_key    = 'product_id';
    }

    function get_child_category($category_id, $inc_parent = false){
        $this->db->where('parent_id', $category_id);
        if ($inc_parent){
            $this->db->or_where('category_id', $category_id);
        }
        return $this->db->select('*')
                        ->from(TBL_PRODUCT_CATEGORY)
                        ->get()
                        ->result_array();
    }

    function get_product_by_category($category_id_array = array(), $limit = 0, $offset = 0, $orderby = 'category_id', $orderway = 'ASC'){
        if ($limit != 0){
            if ($offset != 0){
                $this->db->limit($limit, $offset);
            } else {
                $this->db->limit($limit);
            }
        }

        return $this->db->select('*')
                        ->from(TBL_PRODUCT)
                        ->where('active_flag', ACTIVE_FLAG)
                        ->where('delete_flag', UNDELETE_FLAG)
                        ->where_in('category_id', $category_id_array)
                        ->order_by($orderby.' '.$orderway)
                        ->get()
                        ->result_array();
    }

    function get_product($product_id = 0, $category_id = 0, $limit = 0, $offset = 0, $orderby = NULL, $orderway = 'ASC', $active = ACTIVE_FLAG){
        $this->db->select('*')->from(TBL_PRODUCT);
        $this->db->where('delete_flag', UNDELETE_FLAG);
        $this->db->where('draft', 'no');
        if ($active != ''){
            $this->db->where('active_flag', $active);
        }

        if ($product_id != 0) {
            $this->db->where('product_id', $product_id);
            return $this->db->get()->row_array();
        } else {
            if ($category_id != 0) {
                $this->db->where('category_id', $category_id);
            }

            if ($limit != 0){
                if ($offset != 0) {
                    $this->db->limit($limit, $offset);
                } else {
                    $this->db->limit($limit);
                }
            }

            if ($orderby != NULL) {
                $this->db->order_by($orderby. ' '.$orderway);
            }

            return $this->db->get()->result_array();
        }
    }

    function get_product_hot($category_id = 0, $limit = 0){
        $this->db->select('*');
        $this->db->from(TBL_PRODUCT);
        $this->db->where('active_flag', ACTIVE_FLAG);
        $this->db->where('delete_flag', UNDELETE_FLAG);
        $this->db->where('hot_flag', 1);
        $this->db->order_by('position ASC');

        if ($category_id != 0) {
            $this->db->where('category_id', $category_id);
        }

        if($limit != 0) {
            $this->db->limit($limit);
        }
        return $this->db->get()->result_array();
    }

    function get_product_promotion($limit = 6){
        return $this->db->select('*')
                        ->from(TBL_PRODUCT)
                        ->where('discount >', 0)
                        ->where('active_flag', ACTIVE_FLAG)
                        ->where('delete_flag', UNDELETE_FLAG)
                        ->order_by('discount DESC, title ASC')
                        ->get()
                        ->result_array();
    }

    function get_product_category($limit = 6){
        $category_data = $this->db->select('category_id, title')
                                  ->from(TBL_PRODUCT_CATEGORY)
                                  ->where('home_flag', ACTIVE_FLAG)
                                  ->order_by('category_id ASC')
                                  ->get()
                                  ->result_array();
        $category_id_array = array();
        $category_info     = array();
        foreach($category_data as $key => $value){
            $category_id_array[] = $value['category_id'];
            $category_info[$value['category_id']] = $value['title'];
        }
        $category_id_array = array_unique($category_id_array);
        if (count($category_id_array) > 0) {
            $return['category_info'] = $category_info;
            $return['product'] = $this->db
                                      ->select('*')
                                      ->from(TBL_PRODUCT)
                                      ->where_in('category_id', $category_id_array)
                                      ->where('active_flag', ACTIVE_FLAG)
                                      ->where('delete_flag', UNDELETE_FLAG)
                                      ->order_by('category_id ASC, discount DESC, title ASC')
                                      ->get()
                                      ->result_array();
            return $return;
        }
        return false;
    }

    function get_product_by_manufacturer($manufacturer_id = 0){
        return $this->db
                    ->select('*')
                    ->from(TBL_PRODUCT)
                    ->where('active_flag', ACTIVE_FLAG)
                    ->where('delete_flag', UNDELETE_FLAG)
                    ->where('manufacture', $manufacturer_id)
                    ->get()
            ->result_array();
    }

    function get_manufacturer($manufacturer_id = 0){
        $this->db->select('*')->from(TBL_MANUFACTURER)->where('delete_flag', UNDELETE_FLAG);
        if ($manufacturer_id != 0){
            return $this->db->where('manufacture_id', $manufacturer_id)
                     ->get()
                     ->row_array();
        }
        return $this->db->get()->result_array();
    }

    function get_category($category_id = 0, $parent_id = NULL){
        $this->db->select('*')->from(TBL_PRODUCT_CATEGORY)->where('delete_flag', UNACTIVE_FLAG)->order_by('parent_id ASC, title ASC');

        if (!is_null($parent_id)){
            $this->db->where('parent_id', $parent_id);
        }
        if ($category_id != 0){
            return $this->db->where('category_id', $category_id)->get()->row_array();
        } else {
            return $this->db->get()->result_array();
        }
    }

    function update2($info){
        if (is_array($info)){
            if (isset($info['product_id'])){
                $this->db->where('product_id', $info['product_id'])
                         ->update(TBL_PRODUCT, $info);
                return $info['product_id'];
            } else {
                $this->db->insert(TBL_PRODUCT, $info);
                return $this->db->insert_id();
            }
        }
        return false;
    }

    function update_category($info){
        if (is_array($info)){
            if (isset($info['category_id'])){
                $this->db->where('category_id', $info['category_id'])
                ->update(TBL_PRODUCT_CATEGORY, $info);
                return $info['category_id'];
            } else {
                $this->db->insert(TBL_PRODUCT_CATEGORY, $info);
                return $this->db->insert_id();
            }
        }
        return false;
    }

    function update_manufacturer($info){
        if (is_array($info)){
            if (isset($info['manufacture_id'])){
                $this->db->where('manufacture_id', $info['manufacture_id'])
                ->update(TBL_MANUFACTURER, $info);
                return $info['manufacture_id'];
            } else {
                $this->db->insert(TBL_MANUFACTURER, $info);
                return $this->db->insert_id();
            }
        }
        return false;
    }

    function _delete_category($id){
        if (is_array($id) && count($id) > 0){
            $this->db->where_in('category_id', $id)->update(TBL_PRODUCT_CATEGORY, array('delete_flag' => DELETE_FLAG));
            return true;
        }
        return false;
    }

    function _delete_product($id){
        if (is_array($id) && count($id) > 0){
            $this->db->where_in('product_id', $id)->update(TBL_PRODUCT, array('delete_flag' => DELETE_FLAG));
            return true;
        }
        return false;
    }

    function _delete_manufacturer($id_array){
        if (is_array($id_array) && (count($id_array) > 0)){
            $this->db->where_in('manufacture_id', $id_array)->update(TBL_MANUFACTURER, array('delete_flag' => DELETE_FLAG));
            return true;
        }
        return false;
    }

    function get_images($product_id = 0){
        if ($product_id != 0){
            return $this->db->get_where('gallery', array('product_id' => $product_id))->result_array();
        }
        return false;
    }

    function remove_image($gallery_id = 0){
        if ($gallery_id != 0){
            $data = $this->db
                         ->select('*')
                         ->from('gallery')
                         ->where('gallery_id', $gallery_id)
                         ->get()->row_array();
            $real_file = $_SERVER['DOCUMENT_ROOT'].'/uploads/product/'.$data['product_id'].'/'.$data['image'];
            @unlink($real_file);
            $this->db->where('gallery_id', $gallery_id)->delete('gallery');
            return true;
        }
        return false;
    }

    function search($keyword = '', $condition = NULL){
        if (is_array($condition)){
            $this->db->where($condition);
        }
        return $this->db->select('*')
                        ->from(TBL_PRODUCT)
                        //->where("MATCH (title, description) AGAINST (".$this->db->escape($keyword.'*')." IN BOOLEAN MODE)", NULL, FALSE)
                        ->or_like('title', $keyword)
                        ->or_like('description', $keyword)
                        ->get()
                        ->result_array();
    }
}
